
var requests = require('../../requests/request.js');
var utils = require('../../utils/util.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    activeIndex: 0,    //当前题目下标
    totalScore: 0,      //总得分数
    questionData: {
      question_list: [
        {
          content: ''
        }
      ]
    },                  //所有试题列表
    flag: false,
    testId: '',           //这套试题的id
    testSkipType: 0,       //试题的跳转类型 1、顺序跳转  2、根据答案进行跳转
    toNextQues: 0          //跳转到下一题的题目
  },
  onLoad: function (option) {
    var that = this
    that.setData({
      testSkipType: option.type,
      testId: option.id
    })
    // console.log(option);
    requests.requestQUestionList(   //请求问题 列表数据
      { test_id: option.id },
      (data) => {
        // console.log(data)
        that.setData({
          flag: true,
          questionData: data
        })
      }, () => {
        //请求失败
      }, () => {
        //请求完成
      }
    )
  },
  //选择答案选项
  selectAnswer: function (e) {
    // console.log(e.currentTarget.dataset)
    var that = this;
    var e = e.currentTarget.dataset;
    var activeIndex = that.data.activeIndex;        //当前题目的下标
    // var isAddScore = that.data.isAddScore;
    var testSkipType = that.data.testSkipType;     //测试题的类型 1 是按顺序一次答题  2是跳转型答题顺序
    // console.log(testSkipType) 
    if (testSkipType == '1') {
      var activeScore = parseInt(e.score);    //当前选项的分数
      var totalScore = that.data.totalScore;       //总分数
      var totalNum = that.data.questionData.test_count;         //总的题目数
      if (activeIndex < (totalNum - 1)) {    //判断小于试题总数时 下标activeIndex++
        that.setData({
          totalScore: totalScore + activeScore,
          activeIndex: activeIndex + 1
        })
      } else if (activeIndex == (totalNum - 1)) {   //判断等于试题总数时    
        that.setData({
          totalScore: totalScore + activeScore
        })
        var score = that.data.totalScore;
        var test_id = that.data.testId;
        //跳转到结果页  关闭本页面
        wx.redirectTo({
          url: '../testresult/testresult?test_id=' + test_id + '&score=' + score
        })
      }
    } else {
      var toNextQues = e.tonextques - 1;    //下道题的下标
      var isResult = e.is_result;      //是否是最后一道题
      // console.log(isResult,toNextQues)
      if (!isResult) {
        that.setData({
          activeIndex: toNextQues
        })
      } else {
        var test_id = that.data.testId;         //测试题目的id
        var choiceResult = e.choiceresult;    //最后一题的结果  跳转型试题只提交最后一题的结果
        // console.log(choiceResult)
        //跳转到结果页  关闭本页面
        wx.redirectTo({
          url: '../testresult/testresult?test_id=' + test_id + '&score=' + choiceResult
        })
      }
    }


  }
  //上一题
  // preQuestion:function(){
  //   var that = this;
  //   var activeIndex = that.data.activeIndex;
  //   var totalNum = that.data.questionData.test_count;
  //   var isAddScore = that.data.isAddScore;
  //   var totalScore = that.data.totalScore;
  //   that.setData({    //隐藏提交按钮
  //     isSubmit:true,
  //     isAddScore:true
  //   })
  //   if(activeIndex > 0){     //判断下标activeIndex 等于0时 不再自减
  //     that.setData({
  //       activeIndex:activeIndex - 1
  //     })
  //   }else if(activeIndex == 0){
  //     that.setData({
  //       activeIndex:activeIndex
  //     })
  //   }

  // }
})